package com.tianshi.order.controller;


import com.tianshi.entity.Orders;
import com.tianshi.entity.Users;
import com.tianshi.order.service.OrderItemService;
import com.tianshi.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;


    private Long getLoginUserId(HttpSession session){
        Users user =(Users) session.getAttribute("loginUser");
        if(user==null){
            log.info("用户未登录");
            return null;
        }
        log.info("当前用户:{}",user.getUserId());
        return user.getUserId();
    }


    /**
     * 新增生成订单项数据
     *
     */
    @PostMapping("/pay/{cartId}")
    public ResponseEntity<String> addOrderItem(@PathVariable("cartId") Long cartId) {

        log.info("新增订单项数据，购物车ID：{}", cartId);
        int i = orderService.addOrder(cartId);
        if (i > 0) {
            return ResponseEntity.ok("success");
        }
        return ResponseEntity.badRequest().body("fail");
    }


    /**
     * 查看所有订单 - 管理员
     */
    @GetMapping("/check")
    public ResponseEntity<List<Orders>> checkOrders(){
        log.info("查看所有订单详情");
        List<Orders> orders = orderService.checkOrders();
        return ResponseEntity.ok(orders);
    }

    /**
     * 根据cartId查询订单-用户
     */
    @GetMapping("/check/{cartId}")
    public ResponseEntity<List<Orders>>checkOrderById(@PathVariable("cartId") Long cartId) {
        log.info("查看单个用户订单详情");
        List<Orders> ordersList = orderService.checkOrderById(cartId);
        if (ordersList == null){
            return ResponseEntity.badRequest().body(null);
        }
        return ResponseEntity.ok(ordersList);
    }
    /**
     * 根据订单号查询订单-单个用户
     */

    /**
     * 取消订单 - 根据订单号
     */
    @GetMapping("/cancel/{orderId}")
    public ResponseEntity<String> cancelOrders(@PathVariable ("orderId") Long orderId){
        log.info("取消订单");
        String s = orderService.cancelByOrderSn(orderId);
        if (s.equals("success")){
            return ResponseEntity.ok("success");
        }
        return ResponseEntity.badRequest().build();
    }

    /**
     * 确认收货 - 根据订单号
     *
     */
    @GetMapping("/confirm/{orderId}")
    public ResponseEntity<String> confirmOrders(@PathVariable ("orderId") Long orderId){
        log.info("进入收货");
        String s = orderService.confirmByOrderId(orderId);
        if (s.equals("success")){
            log.info("确认收货成功");
            return ResponseEntity.ok("success");
        }else {
            if (s.equals("订单不存在")){
                log.info("订单不存在");
                return ResponseEntity.ok("订单不存在");
            }else if (s.equals("订单状态不是未收货状态，不能确认收货")){
                log.info("订单状态不是未收货状态，不能确认收货");
                return ResponseEntity.ok("订单状态不是未收货状态，不能确认收货");
            }
        }
        return ResponseEntity.badRequest().build();
    }


    /**
     * 根据userId查询订单
     */
    @GetMapping("/check/user")
    public ResponseEntity<List<Orders>> checkOrderByUserId(HttpSession session) {
        Long userId =getLoginUserId(session);
        log.info("查看用户订单详情");
        List<Orders> ordersList = orderService.checkOrderByUserId(userId);
        if(ordersList.isEmpty()){
            return ResponseEntity.badRequest().body(null);
        }
        return ResponseEntity.ok(ordersList);
    }

    /*
     * 立即购买
     * */
    @GetMapping("/buyOne/{productId}")
    public ResponseEntity<String> buyOne(@PathVariable("productId") Long productId,HttpSession session) {
        log.info("立即购买：{}", productId);
        Long userId = getLoginUserId(session);
        Integer res = orderService.buyOne(productId,userId);

        if (res > 0) {
            return ResponseEntity.ok("success");
        }
        return ResponseEntity.badRequest().body("error");
    }
}
